﻿var Theodoi = function () {
    lststatus = [];
    chitieu = 0;
    dvt = 0;
    status_namkh = 0;
    function round(value, decimals) {
        return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
    }
    function getttangthai(statuid, flag) {
        var name = "";
        if (flag == 1) {
            statuid += 10;
        }
        $.each(lststatus, function (i, v) {
            if (v.STATUS_ID == statuid) {
                name = v.STATUS_NAME;
                return false;
            }
        });
        return name;
    }
    function convertdatatime(data) {
        var value = new Date
                    (
                         parseInt(data.replace(/(^.*\()|([+-].*$)/g, ''))
                    );
        var dat = value.getMonth() +
                                 1 +
                               "/" +
                   value.getDate() +
                               "/" +
               value.getFullYear();
        var datnew = dat.split('/');
        return datnew[1] + '/' + datnew[0] + '/' + datnew[2];
    }
    function loaddatatable(data, flag, initData,mockiemsoat) {
        $('#body').empty();
        if (flag == 0) {
            $.each(data, function (i, v) {
                var row = '<tr class="webgrid-row-style">\
                <td class="clsSTT" style="text-align: center; width: 30px;">\
                    '+ (i + 1) + '\
                </td>\
                <td>\
                    <div id="spnFullName" class="display-mode">\
                        <span class="hidden-780 tenbc">' + v.NAM_KE_HOACH + '</span>\
                    </div>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline">\
                        Kế hoạch năm '+ v.NAM_KE_HOACH + '\
                    </span>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline">\
                    ' + convertdatatime(v.STATUS_DATE) + '\
                    </span>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline" style="color:red;">\
                        ' + mockiemsoat + '\
                    </span>\
                </td>\
                <td class="clsAction">\
                    <div style="text-align:center;">\
                        <div class="btn-group">\
                            <a href="#" data-toggle="dropdown" class="btn btn-lg" style="margin:0px; padding:0px;">\
                                <div class="label label-info">\
                                    <i class="fa fa-th"></i>\
                                </div>\
                            </a>\
                            <ul class="dropdown-menu no-angle pull-right right">\
                                <li class="uploadstatus" style="display:none;">\
                                    <a data="' + v.NAM_KE_HOACH_ID + '" data-nk="' + v.NAM_KE_HOACH + '" data-upload="' + v.STATUS_ID + '" class="sendstatus" style="cursor:pointer;"><i class="fa fa-cloud-upload"></i> ' + getttangthai(v.STATUS_ID, 1) + '</a>\
                                </li>\
                                <li>\
                                    <a data="' + v.NAM_KE_HOACH_ID + '" class="update" style="cursor:pointer;"><i class="fa fa-pencil"></i> Xem thông tin đã chốt</a>\
                                </li>\
                                <li>\
                                    <a data="' + v.NAM_KE_HOACH_ID + '" class="delete" style="cursor:pointer;"> <i class="fa fa-trash-o"></i> Xóa thông tin</a>\
                                </li>\
                            </ul>\
                        </div>\
                    </div>\
                </td>\
            </tr>';
                $('#body').append(row);
            });
        } else {
            var row = '<tr class="webgrid-row-style">\
                <td class="clsSTT" style="text-align: center; width: 30px;">\
                    1\
                </td>\
                <td>\
                    <div id="spnFullName" class="display-mode">\
                        <span class="hidden-780 tenbc">' + data.NAM_KE_HOACH + '</span>\
                    </div>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline">\
                        Kế hoạch năm '+ data.NAM_KE_HOACH + '\
                    </span>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline">\
                    ' + convertdatatime(data.STATUS_DATE) + '\
                    </span>\
                </td>\
                <td class="clsUserName hidden-480">\
                    <span id="spnUserName" class="display-mode timeline" style="color:red;">\
                        ' + mockiemsoat + '\
                    </span>\
                </td>\
                <td class="clsAction">\
                    <div style="text-align:center;">\
                        <div class="btn-group">\
                            <a href="#" data-toggle="dropdown" class="btn btn-lg" style="margin:0px; padding:0px;">\
                                <div class="label label-info">\
                                    <i class="fa fa-th"></i>\
                                </div>\
                            </a>\
                            <ul class="dropdown-menu no-angle pull-right right">\
                            </ul>\
                        </div>\
                    </div>\
                </td>\
            </tr>';
            $('#body').append(row);

        }
        //if (initData.MaDV == 'KHTC') {
        //    $('.uploadstatus').show();
        //    if (data.STATUS_ID == 20) {
        //        $.ajax({
        //            url: initData.GetDonviChuaPhanHoi,
        //            type: "GET",
        //            headers: {
        //                'VerificationToken': initData.VerificationToken
        //            },
        //            dataType: "json",
        //            data: {
        //                'namkh': data.NAM_KE_HOACH_ID,
        //                'status': 21
        //            },
        //            success: function (data) {
        //                $('#thongbao').text("");
        //                if (data.length > 0) {
        //                    var thongbao = "Danh sách các trường chưa phản hồi: ";
        //                    $.each(data, function (i, v) {
        //                        thongbao += v.DON_VI + ", ";
        //                    });
        //                    $('#thongbao').text(thongbao);
        //                }
        //            },
        //            error: function (jqXHR, textStatus, errorThrown) {
        //                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
        //            },
        //            complete: function () {
        //            }
        //        });
        //    } else {
        //        $('#thongbao').text("");
        //    }
        //} else {
        //    $('.uploadstatus').hide();
        //}
    }
    function Loaddata(data, initData) {
        if (data == -1) {
            var lst = [];
            status_namkh = 0;
            $.ajax({
                url: initData.GetallNamkehoach,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                success: function (data) {
                    $('#Namlapbc').empty();
                    $('#Namlapbc').append('<option value="-1">[--Chọn năm kế hoạch--]</option>');
                    $('#mocks').empty();
                    $('#mocks').append('<option value="-1">[--Chọn mốc kiểm soát--]</option>');
                    $.each(data, function (i, v) {
                        if (v.STATUS_ID >= 40) {
                            $('#Namlapbc').append('<option value="' + v.NAM_KE_HOACH_ID + '">' + v.NAM_KE_HOACH + '</option>');
                            lst.push(v);
                        }
                    });
                    data = lst;
                    loaddatatable(data, 0, initData, "");
                    $('#Create').prop("disabled", true);


                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        } else {
            $('#Create').prop("disabled", false);
            $.ajax({
                url: initData.GetNamkehoachbyID,
                type: "GET",
                async: false,
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'id': data
                },
                success: function (data1) {
                    status_namkh = data1.STATUS_ID;
                    if (data1.STATUS_ID >= 40) {
                        $.ajax({
                            url: initData.Getmockiemsoatbynam,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': data
                            },
                            success: function (data2) {
                                $('#mocks').empty();
                                $('#mocks').append('<option value="-1">[--Chọn mốc kiểm soát--]</option>');
                                $.each(data2, function (i, v) {
                                    $('#mocks').append('<option value="' + v.MOC_KIEM_SOAT_ID + '">' + v.MOC_KIEM_SOAT + '</option>');
                                });
                                $('#mocks').append('<option value="-2">[--Tổng thể--]</option>');
                                $.ajax({
                                    url: initData.Getmockiemsoatactivebynam,
                                    type: "GET",
                                    headers: {
                                        'VerificationToken': initData.VerificationToken
                                    },
                                    dataType: "json",
                                    data: {
                                        'namkh': data
                                    },
                                    success: function (data) {
                                        if (data.MOC_KIEM_SOAT_ID > 0) {
                                            $('#mocks').val(data.MOC_KIEM_SOAT_ID);
                                        } else {
                                            $('#Create').prop("disabled", true);
                                        }
                                        loaddatatable(data1, 1, initData, data.MOC_KIEM_SOAT);
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                    },
                                    complete: function () {
                                    }
                                });

                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        }
    }
    function calculateSum(a) {
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".restartall").each(function () {
            if ($(this).attr('name') == a) {
                //add only if the value is number
                if (!isNaN(this.value) && this.value.length != 0) {
                    sum += parseFloat(this.value);
                }
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $('.' + a + '').val(sum);
    }
    function loaddatatotable(nhomchitieu, nhomdonvi, dt, initData, nkhid, flag) {
        var lstchitieucap2;
        var lstdonvi = [];
        if (flag == 0) {
            $.ajax({
                url: initData.GetallChitieubynamchitieu,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': nkhid,
                    'madonvi': initData.DonviID
                },
                success: function (data) {
                    lstchitieucap2 = data;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            $.ajax({
                url: initData.GetAllDonViByNhomID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'nhomdvid': nhomdonvi
                },
                success: function (data) {
                    lstdonvi = data;
                    if (data.length > 6) {
                        $('#Print').prop('disabled',true);
                    } else {
                        $('#Print').prop('disabled', false);
                    }
                    if (data.length >= 4 && data.length <=8) {
                        $('#tbdata').css('width', '3000px');
                        $('.chitieun').css('width', '7%');
                        chitieu = 15;
                        dvt = 3;
                        $('.dvt').css('width', '2%');
                    }
                    if (data.length < 4) {
                        $('#tbdata').css('width', '1110px');
                        $('.chitieun').css('width', '22%');
                        $('.dvt').css('width', '7%');
                        chitieu = 27;
                        dvt = 7;
                    }
                    if (data.length >= 9) {
                        $('#tbdata').css('width', '6000px');
                        $('.chitieun').css('width', '7%');
                        $('.dvt').css('width', '2%');
                        chitieu = 7;
                        dvt = 2;
                    }
                    
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
        } else {
            $.ajax({
                url: initData.GetallChitieubynamchitieubydonvi,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': nkhid,
                    'madonvi': nhomdonvi
                },
                success: function (data) {
                    lstchitieucap2 = data;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            $.ajax({
                url: initData.GetDonViID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'donviid': nhomdonvi
                },
                success: function (data) {
                    lstdonvi.push(data);
                    $('#tbdata').css('width', '1110px');
                    
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
        }

        var check = 0;
        $.ajax({
            url: initData.CheckChitieubymacap1,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            data: {
                'mapcap1': nhomchitieu
            },
            success: function (data) {
                check = data.TYPE;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });

        if (check == 2) {
            $('#hhh').empty();
            $('#bdy').empty();
            var dt = " <tr>";
            dt += '<th rowspan="2" class="chitieun" style=" width: 300px">' + $('#nhomchitieu option:selected').text().toUpperCase() + '</th>\
               <th rowspan="2" class="dvt" style=" width: 4%;">Đơn vị tính</th>';
            var dx = '<tr>';
            $.each(lstdonvi, function (i, v) {
                dt += '<th colspan="3" style="background-color:#C9E8D1" class="ct1">' + v.TenVietTat + '</th>';
                dx += '<th>KH</th>\
                        <th>TH</th>\
                        <th>%</th>';
            })
            dx += "</tr>";
            dt += "</tr>";
            $('#hhh').append(dt);
            $('#hhh').append(dx);
            $.each(lstchitieucap2, function (i, v) {
                if (v.CAP == 2 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td style="font-weight:bold;">' + v.TEN_CHI_TIEU + '</td>\
                                <td></td>';
                    $.each(lstdonvi, function (i, v) {
                        dt += '<td></td>\
                                <td></td>\
                                <td></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);
                }
                if (v.CAP == 3 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td>' + v.TEN_CHI_TIEU + '</td>\
                        <td>' + v.DON_VI_TINH + '</td>';
                    $.each(lstdonvi, function (y, z) {
                        dt += '<td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="1" style="width: 100%;margin-left: 0%;" value="" /></td>\
                           <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '"  disabled="disabled" class="e-input1 form-control restartall" level="2" style="width: 100%;margin-left: 0%;" value="" /></td>\
                           <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '"  disabled="disabled" class="e-input1 form-control restartall" level="7" style="width: 100%;margin-left: 0%;" value="" /></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);

                }
            });
            $.ajax({
                url: initData.GetKhRiengbyDV,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkh': $.session.get('nkhid'),
                    'status': 21
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetKhRiengbyDV,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkh': $.session.get('nkhid'),
                    'status': 51
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.Getkehoachthrieng,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkh': $.session.get('nkhid'),
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="2"]').val(numeral(v.GIA_TRI).format('0,0'));
                        var b = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="2"]').val();
                        if (b != "" && b != "0" && b != undefined) {
                            var a = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="1"]').val();
                            if (a != "" && a != "0" && a != undefined) {
                                var c = round((parseInt(b.replace(",", "")) / parseInt(a.replace(",", ""))) * 100,2);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="7"]').val(c);
                            }
                        }
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $("#tbdata").tableHeadFixer({ "left": 2 });
        } else {
            $('#hhh').empty();
            $('#bdy').empty();
            var dt = " <tr>";
            dt += '<th rowspan="2" class="chitieun" style=" width:300px;">' + $('#nhomchitieu option:selected').text().toUpperCase() + '</th>\
               <th rowspan="2" class="dvt" style=" width: ' + dvt + '%;">Đơn vị tính</th>\
               <th colspan="5" style="background-color:#C9E8D1" class="ct1 dhqg">ĐHQG</th>';
            var dx = '<tr>\
                    <th>KH</th>\
                    <th class="dc">ĐC</th>\
                    <th>Tổng TH</th>\
                    <th>% (KH)</th>\
                    <th class="dc">% (ĐC)</th>\
            ';
            $.each(lstdonvi, function (i, v) {
                dt += '<th colspan="3" style="background-color:#C9E8D1" class="ct1">' + v.TenVietTat + '</th>';
                dx += '<th>KH</th>\
                    <th>TH</th>\
                    <th>%</th>';
            })
            dx += "</tr>";
            dt += "</tr>";
            $('#hhh').append(dt);
            $('#hhh').append(dx);
            $.each(lstchitieucap2, function (i, v) {
                if (v.CAP == 2 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td style="font-weight:bold;">' + v.TEN_CHI_TIEU + '</td>\
                                <td></td>\
                                <td></td>\
                                <td class="dc"></td>\
                                <td></td>\
                                <td></td>\
                                <td class="dc"></td>';
                    $.each(lstdonvi, function (i, v) {
                        dt += '<td></td>\
                                <td></td>\
                    <td></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);
                }
                if (v.CAP == 3 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td>' + v.TEN_CHI_TIEU + '</td>\
                        <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="3" value="" /></td>\
                                <td class="dc"><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="5" value="" /></td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="15" value="" /></td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="4" value="" /></td>\
                                <td class="dc"><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="6" value="" /></td>\
                                ';
                    $.each(lstdonvi, function (y, z) {
                        dt += '<td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall" level="1" value="" /></td>\
                           <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall" level="2" value="" /></td>\
                           <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall" level="7" value="" /></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);

                }
            });

            
            if (flag == 0) {
                if (parseInt(status_namkh) < 70) {
                    $.ajax({
                        url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                        type: "GET",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        async: false,
                        data: {
                            'namkh': $.session.get('nkhid'),
                            'nhomdv': nhomdonvi,
                            'nhomct': nhomchitieu,
                            'status': 31,
                            'flag': 0
                        },
                        success: function (data) {
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                            })
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                if (parseInt(status_namkh) >= 70) {
                    $.ajax({
                        url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                        type: "GET",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        async: false,
                        data: {
                            'namkh': $.session.get('nkhid'),
                            'status': 61,
                            'nhomdv': nhomdonvi,
                            'nhomct': nhomchitieu,
                            'flag': 0
                        },
                        success: function (data) {
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                            })
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                $.ajax({
                    url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'status': 0,
                        'nhomdv': nhomdonvi,
                        'nhomct': nhomchitieu,
                        'flag': 0
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val(numeral(v.GIA_TRI).format('0,0'));
                        })

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });

                $.ajax({
                    url: initData.GetallKHTHNEW,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'flag': '0',
                        'mocks': $("#mocks").val(),
                        'nhomdonvi': $('#nhomdonvi').val()
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val(numeral(v.GIA_TRI).format('0,0'));
                            var b = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val();
                            if (b != "" && b != "0" && b != undefined) {
                                var a = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val();
                                if (a != "" && a != "0" && a != undefined) {
                                    var c = round((parseInt(b.replace(",", "")) / parseInt(a.replace(",", ""))) * 100,2);
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="7"]').val(c);
                                }
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
                $.ajax({
                    url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'status': 0,
                        'nhomdv': nhomdonvi,
                        'nhomct': nhomchitieu,
                        'flag': 0
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val(numeral(v.GIA_TRI).format('0,0'));
                        })

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            } else {
                if (parseInt(status_namkh) < 70) {
                    $.ajax({
                        url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                        type: "GET",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        async: false,
                        data: {
                            'namkh': $.session.get('nkhid'),
                            'nhomdv': $('#donvi').val(),
                            'nhomct': nhomchitieu,
                            'status': 31,
                            'flag': 1
                        },
                        success: function (data) {
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                            })
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                if (parseInt(status_namkh) >= 70) {
                    $.ajax({
                        url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                        type: "GET",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        async: false,
                        data: {
                            'namkh': $.session.get('nkhid'),
                            'status': 61,
                            'nhomdv': $('#donvi').val(),
                            'nhomct': nhomchitieu,
                            'flag': 1
                        },
                        success: function (data) {
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(numeral(v.GIA_TRI).format('0,0'));
                            })
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                

                $.ajax({
                    url: initData.GetallKHTHNEW,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'flag': '1',
                        'mocks': $("#mocks").val(),
                        'nhomdonvi': $('#donvi').val()
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val(numeral(v.GIA_TRI).format('0,0'));
                            var b = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val();
                            if (b != "" && b != "0" && b != undefined) {
                                var a = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val();
                                if (a != "" && a != "0" && a != undefined) {
                                    var c = round((parseInt(b.replace(",", "")) / parseInt(a.replace(",", ""))) * 100,2);
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="7"]').val(c);
                                }
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
                $.ajax({
                    url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'status': 0,
                        'nhomdv': 2,
                        'nhomct': nhomchitieu,
                        'flag': 3
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val(numeral(v.GIA_TRI).format('0,0'));
                        })

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            }
            
            $.ajax({
                url: initData.GetSumKH,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'statusid': 61,
                },
                success: function (data) {
                    if (data.length > 0) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="5"]').val(numeral(v.GIA_TRI).format('0,0'));
                        });
                        $('.dhqg').attr('colspan', 5);
                        $('.dc').show();
                    } else {
                        $('.dc').hide();
                        $('.dhqg').attr('colspan', 3);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetSumKHTH,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'flag': '0'
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        var chinhthuc = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val();
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val(numeral(v.GIA_TRI).format('0,0'));
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="15"]').val(numeral(v.GIA_TRI).format('0,0'));
                        if (chinhthuc != "" && chinhthuc != "0" && chinhthuc != undefined) {
                            var ht1 = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val();
                            if (ht1 != "" && ht1 != "0" && ht1 != undefined) {
                                var c = round((parseInt(ht1.replace(",", "")) / parseInt(chinhthuc.replace(",", ""))) * 100,2);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val(c);
                            }
                        }
                        var dinhhuong = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="5"]').val();
                        if (dinhhuong != "" && dinhhuong != "0" && dinhhuong != undefined) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val(numeral(v.GIA_TRI).format('0,0'));
                            var ht2 = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val();
                            if (ht2 != "" && ht2 != "0" && ht2 != undefined) {
                                var c = round((parseInt(ht2.replace(",", "")) / parseInt(dinhhuong.replace(",", ""))) * 100,2);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val(c);
                            }
                        }
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $("#tbdata").tableHeadFixer({ "left": 2 });
        }
        if (flag != 0) {
            $('.chitieun').css('width', '27%');
            $('.dvt').css('width', '7%');
        }

    }
    var initLoadData = function (initData) {
        $.ajax({
            url: initData.Getallstatus,
            type: "GET",
            async: false,
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            success: function (data) {
                lststatus = data;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
        $.ajax({
            url: initData.GetallNamkehoach,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            success: function (data) {
                var lst = [];
                $('#Namlapbc').empty();
                $('#Namlapbc').append('<option value="-1">[--Chọn năm kế hoạch--]</option>');
                $.each(data, function (i, v) {
                    if (v.STATUS_ID >= 40) {
                        $('#Namlapbc').append('<option value="' + v.NAM_KE_HOACH_ID + '">' + v.NAM_KE_HOACH + '</option>');
                        lst.push(v);
                    }
                });
                var namkh = $.cookie('mkhidchung');
                data = lst;
                if (namkh != undefined) {
                    $('#Namlapbc').val(namkh);
                    Loaddata(namkh, initData);
                    $.session.set('nkhid', namkh);

                }
                else {
                    loaddatatable(data, 0, initData,"");
                }
                if (initData.MaDV == "KHTC") {
                    $('.uploadstatus').show();
                } else {
                    $('.uploadstatus').hide();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });

    }
    var initButtonClick = function (initData) {
        var stus = 10;
        var fl = 0;
        $('#Namlapbc').on('change', function () {
            var namkh = $(this).val();
            Loaddata(namkh, initData);
            $.session.set('nkhid', namkh);
            $.cookie('mkhidchung', $(this).val());
        })

        $('#Create').on('click', function () {
            stus = 10;
            $('#myModalLabel').text('Theo dõi thực hiện kế hoạch - ' + $("#mocks option:selected").text());
            var namkehoachid = $('#Namlapbc').val();
            $.ajax({
                url: initData.GetDonViByNhom,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'status': false
                },
                async: false,
                success: function (data) {
                    $('#donvi').empty();
                    $.each(data, function (i, v) {
                        $('#donvi').append('<option value="' + v.DonViId + '">' + v.TenDonVi + '</option>');
                    });
                    if (initData.QUYENGIAMSAT == 'False') {
                        $("#donvi option").each(function () {
                            if ($(this).val() != initData.DonviID) {
                                $(this).remove();
                            }
                        });
                        $('#donvi').val(initData.DonviID);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetallChitieubynamchitieu,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid,
                    'madonvi': initData.DonviID
                },
                success: function (data) {
                    lstnhomchitieu = data;
                    $('#nhomchitieu').empty();
                    $.each(data, function (i, v) {
                        if (v.CAP == 1) {
                            $('#nhomchitieu').append('<option value="' + v.MA_CAP1 + '">' + v.TEN_CHI_TIEU + '</option>')
                        }
                    });

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            $.ajax({
                url: initData.Getallnhomdonvi,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid
                },
                success: function (data) {
                    $('#nhomdonvi').empty();
                    $.each(data, function (i, v) {
                        $('#nhomdonvi').append('<option value="' + v.NhomDonViId + '">' + v.TenNhomDonVi + '</option>')
                    });

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            if (initData.QUYENGIAMSAT == 'False') {
                $('#nhomdonvi').prop('disabled', 'disabled');
                $('#donvi').prop('disabled', 'disabled');
                fl = 1;
                $('.chn').hide();
                $('#uniform-dvn').find('span').addClass('checked');
                $('#dvn').attr('checked', true); 
                $('#nhm').attr('checked', false);
                loaddatatotable($('#nhomchitieu').val(), initData.DonviID, stus, initData, $.session.get('nkhid'), fl);
            } else {
                $('#uniform-dvn').find('span').removeClass('checked');
                $('.chn').show();
                loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, namkehoachid, 0);
            }

            $('#Update').show();
            $('#Chot').show();
            $('#myModal').modal('show');
        });


        $('input[type=radio][name=inlineRadioOptions]').change(function () {
            var dt = $(this).val();
            if (dt == 'nhom') {
                $('#donvi').prop('disabled', 'disabled');
                $('#nhomdonvi').prop('disabled', false);
                fl = 0;
                $("#btnBCDonVi").hide();                
                loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), 0);
            } else {
                fl = 1;
                $('#nhomdonvi').prop('disabled', 'disabled');
                $('#donvi').prop('disabled', false);
                $("#btnBCDonVi").show();                
                loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), fl);
            }
          
        })


        if ($('input[name="inlineRadioOptions"]:checked').val() == 'dv') {
            $("#btnBCDonVi").show();
        } else if ($('input[name="inlineRadioOptions"]:checked').val() == 'nhom') {
            $("#btnBCDonVi").hide();
        }

        $('.update').live('click', function () {
            stus = 11;
            var namkehoachid = $(this).attr('data');
            $.session.set('nkhid', namkehoachid);
            $.ajax({
                url: initData.GetallChitieubynamchitieu,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid,
                    'madonvi': initData.DonviID
                },
                success: function (data) {
                    $('#nhomchitieu').empty();
                    $.each(data, function (i, v) {
                        if (v.CAP == 2) {
                            $('#nhomchitieu').append('<option value="' + v.MA_CAP2 + '">' + v.TEN_CHI_TIEU_CAP2 + '</option>')
                        }
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            $.ajax({
                url: initData.Getallnhomdonvi,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid
                },
                success: function (data) {
                    $('#nhomdonvi').empty();
                    $.each(data, function (i, v) {
                        $('#nhomdonvi').append('<option value="' + v.NhomDonViId + '">' + v.TenNhomDonVi + '</option>')
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, namkehoachid);
            $('#Update').hide();
            $('#Chot').hide();
            $('#myModal').modal('show');
        });
        $('#nhomdonvi').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), 0);
        });
        $('#donvi').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), 1);
        });
        $('#nhomchitieu').on('change', function () {
            if (fl == 0) {
                loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), fl);
            } else {
                loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), fl);
            }

        });
        $('#mocks').live('change', function () {
            if ($(this).val() == -1) {
                $('#Create').prop("disabled", true);
            } else {
                $('#Create').prop("disabled", false);
            }
        })
    }
    return {
        init: function (initData) {
            //load data
            initLoadData(initData);
            initButtonClick(initData);
        }
    }
}();